package com.dayatang.hrm.compensation.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.dayatang.commons.domain.AbstractEntity;
import com.dayatang.commons.domain.InstanceFactory;

/**
 * 薪资数据来源
 * 
 * @author chencao
 * 
 */
@Entity
@Table(name = "pay_ds")
public class SalaryDataSource extends AbstractEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6569594712690106986L;

	private static SalaryDataSourceRepository salaryDataSourceRepository;

	// 数据源名称
	@Column(name = "name", length = 50)
	private String name;
	// 实体全路径名
	@Column(name = "full_entity_name")
	private String fullEntityName;

	@ManyToOne
	@JoinColumn(name = "salary_accountsuite_id")
	private SalaryAccountSuite salaryAccountSuite;

	public SalaryDataSource() {
	}

	public static SalaryDataSourceRepository getSalaryDataSourceRepository() {
		if (salaryDataSourceRepository == null) {
			salaryDataSourceRepository = InstanceFactory
					.getInstance(SalaryDataSourceRepository.class);
		}
		return salaryDataSourceRepository;
	}

	public static void setSalaryDataSourceRepository(
			SalaryDataSourceRepository salaryDataSourceRepository) {
		SalaryDataSource.salaryDataSourceRepository = salaryDataSourceRepository;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFullEntityName() {
		return fullEntityName;
	}

	public void setFullEntityName(String fullEntityName) {
		this.fullEntityName = fullEntityName;
	}

	public SalaryAccountSuite getSalaryAccountSuite() {
		return salaryAccountSuite;
	}

	public void setSalaryAccountSuite(SalaryAccountSuite salaryAccountSuite) {
		this.salaryAccountSuite = salaryAccountSuite;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof SalaryDataSource == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		SalaryDataSource rhs = (SalaryDataSource) obj;
		return new EqualsBuilder().append(getId(), rhs.getId()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	public void save() {
		getSalaryDataSourceRepository().save(this);
	}

	public void remove() {
		getSalaryDataSourceRepository().remove(this);
	}

	public static SalaryDataSource get(Long id) {
		return getSalaryDataSourceRepository().get(id);
	}

	/**
	 * 根据指定日期，找到薪资帐套下的数据来源列表
	 * 
	 * @param accountSuite
	 * @param queryDate
	 * @return
	 */
	public static List<SalaryDataSource> findBySalaryAccountSuite(
			SalaryAccountSuite accountSuite, Date queryDate) {
		return getSalaryDataSourceRepository().findBySalaryAccountSuite(
				accountSuite, queryDate);
	}

}
